import { request } from '../../utils/request'
let startY1, moveY1, moveDistance1, startY2, moveY2, moveDistance2
let that
let there
let defaultUserInfo = {
  avatarUrl:"/static/img/personal/missing-face.png",
  nickname:"游客"
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    transform: "translateY(0)",
    transition:"",
    recentPlayList:[]
    // rotate:"rotateY(0)"
    // transform:`transformY(${moveDistance2})`
  },

  /**
   * 生命周期函数--监听页面加载
   */
  //onLoad上最好不要使用async, 所以recentPlay的数据使用功能函数调用的方式
  onLoad() {
    that = getCurrentPages()[0];
    //当没有存储userInfo数据时, userInfo是空字符串而不是undefined
    const userInfo =wx.getStorageSync('userInfo')
    //constructor属性的值是String类型变量不是字符串, typeof返回的是字符串
    //因为之前存的是Object, 所以如果userInfo.constructor === String 而且userInfo === ''那么说明登录数据没有
    //把本地的userInfo更改为'游客'的userInfo
    if(userInfo.constructor === Object){
      const { userId:uid } = userInfo
      //如果在getRecentPlayDataById函数中没有设置setData, 这边不await则获取的是一个promise对象
      //所以在getRecentPlayDataById中设置好了recentPlayList的值, 这边只需要调用该函数, 不需要返回值
      that.getRecentPlayDataById(uid)
      return that.setData({
        userInfo
      })
    }
    else if(userInfo.constructor === String && userInfo === '') {
      return that.setData({
      userInfo:defaultUserInfo
    })
  }
  },
  getRecentPlayDataById: async (uid) => {
    const recentPlayListData = await request(
      'user/record', {
        uid,
        type:1
      })
      //截取前10条数据, 并且将data里面的song拉出来组成新的数组
      //splice是修改原数组, 并返回新数组(a数组变成新a+新b), slice返回新数组(a数组, 变成a数组+b数组)
    const recentPlayList = recentPlayListData.weekData.slice(0,10).map(item => item.song)
    return that.setData({
      recentPlayList
    })
  },
  handleTouchStart: e => {
    startY1 = e.touches[0].clientY
    that.setData({
      transition:""
    })
    // startY2 = e.touches[0].clientY
  },
  handleTouchMove: e => {
    moveY1 = e.touches[0].clientY
    // moveY2 = e.touches[0].clientY
    moveDistance1 = moveY1 - startY1
    // moveDistance2 = moveY2 - startY2
    moveDistance1 = (moveDistance1 <= 0)?0:((moveDistance1 >= 100)? 100 : moveDistance1)
    that.setData({
      transform:`translateY(${moveDistance1}rpx)`,
      // rotate:`rotateY(${moveDistance2}deg)`
    })
  },
  handleTouchEnd: e => {
    that.setData({
      transform:`translateY(0)`,
      transition:"transform 1s linear"
    })
  },
  nav_login: () => {
    wx.reLaunch({
      url:'/pages/login/login'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})